import { thirdLogin } from '@/api/config'
import state from '@/store/state'
// import wx from 'weixin-js-sdk'
import axios from 'axios'
// import Cookies from 'js-cookie'

// 微信配置
class WxConfig{
  constructor(){
    // 通过config接口注入微信权限验证配置
    this.wxConfig = {
      debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
      appId: '', // 必填，公众号的唯一标识
      timestamp: '', // 必填，生成签名的时间戳
      nonceStr: '', // 必填，生成签名的随机串
      signature: '', // 必填，签名，见附录1
      jsApiList: [] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
    }
    this.jsApiList = ['checkJsApi', 'showMenuItems', 'hideMenuItems', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'chooseImage']
    this.menuList = ['menuItem:copyUrl', 'menuItem:share:appMessage', 'menuItem:share:timeline', 'menuItem:share:qq', 'menuItem:share:weiboApp', 'menuItem:share:QZone']
    this.shareList = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone']
  }

  // 微信授权获取用户信息
  async getUserInfo(vm, href = '', callback) {
    // 授权地址
    let url = href || window.location.origin
    if(!vm.$route.query.code && !Boolean(localStorage.getItem("access_token"))){
      window.location.href = `${state.API_URL}/UserAccount/h5WeChatOauth?redirect_url=${url}`
    } else if (vm.$route.query.code && vm.$route.query.state  && !Boolean(localStorage.getItem("access_token"))) {
      // let v_code = vm.$route.query.code
      // let v_state = vm.$route.query.state
      //localStorage.setItem('code', code)
      let uerInfo = await thirdLogin({
        type: 3,
        code: vm.$route.query.code,
        invitation_code: vm.$route.query.invitation_code || ''
      })
      if (uerInfo.data.code === 0) {
        // 本地储存
        localStorage.setItem('access_token', uerInfo.data.data.access_token)
        localStorage.setItem('user_info', JSON.stringify(uerInfo.data.data.user))
        // 储存到COOKIES，用在服务器渲染
        // Cookies.set('access_token', uerInfo.data.data.access_token)
        // Cookies.set('user_info', JSON.stringify(uerInfo.data.data.user))
        // 授权后做回调
        if (callback) {
          callback()
        }
      }
    }
    //let sCode = Boolean(localStorage.getItem('code'))
    // let sCode = Boolean(vm.$route.query.code)
    // if (!sCode) {
    //   window.location.href = `${state.API_URL}/UserAccount/h5WeChatOauth?redirect_url=${url}`
    // }
  }

  // 获取 微信签名权限验证
  setWxConfig (shareurl) {
    // let shareurl = encodeURIComponent(window.location.href);
    axios.post(`${state.API_URL}/WechatShare/getShareConfig`, {
      url: encodeURIComponent(shareurl)
    }).then((res) => {
      let wxarr = res.data.data.wx_config
      this.wxConfig.appId = wxarr.appId
      this.wxConfig.timestamp = wxarr.timestamp
      this.wxConfig.nonceStr = wxarr.nonceStr
      this.wxConfig.signature = wxarr.signature
      this.wxConfig.jsApiList = this.jsApiList
      wx.config(this.wxConfig);
    })
  }

  // 设置 批量菜单隐藏
  hideMenuItems (menuList) {
    let _self = this
    wx.ready(() => {
      wx.hideMenuItems({
        menuList: _self.menuList,
        fail: fail => {
          // 注入失败重新刷新页面 重新注入
          // setTimeout(res => {
          //   window.location.href = window.location.href
          // }, 50)
        }
      })
    })
  }

  // 设置 批量菜单显示
  showMenuItems (menuList) {
    let _self = this
    wx.ready(res => {
      wx.showMenuItems({
        menuList: _self.menuList,
        fail: fail => {
          // 注入失败重新刷新页面 重新注入
          // setTimeout(res => {
          //   window.location.href = window.location.href
          // }, 50)
        }
      })
    })
  }

  // 设置 批量分享
  setMenuShare (vm, title, link, desc, imgUrl, type = '', dataUrl = '') {
    let _self = this
    wx.ready((res) => {
      _self.shareList.map(share => {
        wx[share]({
          title: title, // 分享标题
          link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          desc: desc, // 分享描述
          imgUrl: imgUrl, // 分享图标，
          type: type,  // 分享类型,music、video或link，不填默认为link
          dataUrl: dataUrl, // 如果type是music或video，则要提供数据链接，默认为空
          success: success => {
            // 用户确认分享后执行的回调函数
          },
          fail: fail => {
            // vm.$vux.toast.show({type: 'cancel', text: '配置出错'})
          }
        })
      })
    })
  }
}

let wxConfig = new WxConfig()

export default wxConfig